package cn.org.timespace.ons.inside.mina.filter.command;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.org.timespace.ons.inside.mina.filter.command.concrete.UpMachinesStatusCommand;
import cn.org.timespace.ons.inside.mina.filter.command.concrete.UpdateCommand;
import cn.org.timespace.ons.inside.mina.filter.command.concrete.UpstatusCommand;

//import com.timespace.socket.mina.filter.command.concrete.ResetConfigInfoCommand;
//import com.timespace.socket.mina.filter.command.concrete.SyncConfigInfoCommand;
//import com.timespace.socket.mina.filter.command.concrete.TimeCheckCommand;
//import com.timespace.socket.mina.filter.command.concrete.UploadAttendanceCommand;
//import com.timespace.socket.mina.filter.command.concrete.upgrade.UpgradeCommand;

public final class ESInCommandFactory {

	private static Map<String, Class<? extends ESInCommand>> registry = new HashMap<String, Class<? extends ESInCommand>>();

	static {
		register("01", UpdateCommand.class);
		register("02", UpstatusCommand.class);
		register("03", UpMachinesStatusCommand.class);
//		register("08", UploadAttendanceCommand.class);
//		register("0C", TimeCheckCommand.class);
//		register("AA", UpgradeCommand.class);
	}

	private static void register(String name, Class<? extends ESInCommand> clazz) {
		if ((name != null) && (clazz != null))
			registry.put(name, clazz);
	}

	public static AbstractESInCommand newCommand(String name) {
		if (StringUtils.isNotEmpty(name)) {
			Class<? extends ESInCommand> clazz = (Class<? extends ESInCommand>) registry.get(name);
			if ((clazz != null) && (AbstractESInCommand.class.isAssignableFrom(clazz))) {
				try {
					return (AbstractESInCommand) clazz.newInstance();
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}
			}
		}

		return null;
	}
}
